package com.swotxu.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swotxu.common.utils.ComparableUtil;
import com.swotxu.common.utils.PageUtils;
import com.swotxu.common.utils.Query;
import com.swotxu.mall.product.dao.SkuInfoDao;
import com.swotxu.mall.product.entity.SkuImagesEntity;
import com.swotxu.mall.product.entity.SkuInfoEntity;
import com.swotxu.mall.product.entity.SpuInfoDescEntity;
import com.swotxu.mall.product.service.AttrGroupService;
import com.swotxu.mall.product.service.SkuImagesService;
import com.swotxu.mall.product.service.SkuInfoService;
import com.swotxu.mall.product.service.SkuSaleAttrValueService;
import com.swotxu.mall.product.service.SpuInfoDescService;
import com.swotxu.mall.product.web.vo.SkuItemSaleAttrVo;
import com.swotxu.mall.product.web.vo.SkuItemVo;
import com.swotxu.mall.product.web.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    ThreadPoolExecutor executor;
    @Autowired
    SkuImagesService skuImagesService;
    @Autowired
    SpuInfoDescService spuInfoDescService;
    @Autowired
    AttrGroupService attrGroupService;
    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * catelogId: 0,
     * brandId: 0,
     * min: 0,
     * max: 0
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        LambdaQueryWrapper<SkuInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        String key = (String) params.get("key");
        lambdaQueryWrapper.and(StringUtils.isNotEmpty(key), wrapper -> {
            wrapper.eq(SkuInfoEntity::getSkuId, key).or().like(SkuInfoEntity::getSkuName, key);
        });

        String brandId = (String) params.get("brandId");
        lambdaQueryWrapper.eq(ComparableUtil.gtZeroOfLong(brandId), SkuInfoEntity::getBrandId, brandId);

        String catelogId = (String) params.get("catelogId");
        lambdaQueryWrapper.eq(ComparableUtil.gtZeroOfLong(catelogId), SkuInfoEntity::getCatalogId, catelogId);

        String min = (String) params.get("min");
        lambdaQueryWrapper.ge(ComparableUtil.gtZeroOfBigDecimal(min), SkuInfoEntity::getPrice, min);

        String max = (String) params.get("max");
        lambdaQueryWrapper.le(ComparableUtil.gtZeroOfBigDecimal(max), SkuInfoEntity::getPrice, max);

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                lambdaQueryWrapper
        );

        return new PageUtils(page);
    }

    public SkuItemVo item_overdue(Long skuId) {
        SkuItemVo skuItemVo = new SkuItemVo();
        // 1、sku基本信息获取 pms_sku_info
        SkuInfoEntity skuInfo = getById(skuId);
        skuItemVo.setSkuInfo(skuInfo);
        Long spuId = skuInfo.getSpuId();
        Long catalogId = skuInfo.getCatalogId();

        // 2、sku图片信息 pms_sku_images
        List<SkuImagesEntity> skuImages = skuImagesService.list(new LambdaQueryWrapper<SkuImagesEntity>().eq(SkuImagesEntity::getSkuId, skuId));
        skuItemVo.setSkuImages(skuImages);

        // 3、sku对应spu的销售属性组合
        List<SkuItemSaleAttrVo> skuSaleAttrs = skuSaleAttrValueService.getSaleAttrsBySupId(spuId);
        skuItemVo.setSkuSaleAttrs(skuSaleAttrs);

        // 4、spu的介绍 pms_spu_info_desc
        SpuInfoDescEntity spuDesp = spuInfoDescService.getById(spuId);
        skuItemVo.setSpuDesp(spuDesp);

        // 5、spu的规格参数信息，即所有属性值
        List<SpuItemAttrGroupVo> spuGroupAttrs = attrGroupService.getAttrGroupWithAttrsBySpuId(spuId, catalogId);
        skuItemVo.setSpuGroupAttrs(spuGroupAttrs);

        return skuItemVo;
    }

    // 异步化改造
    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo = new SkuItemVo();

        // 1、sku基本信息获取 pms_sku_info
        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfo = getById(skuId);
            skuItemVo.setSkuInfo(skuInfo);
            return skuInfo;
        }, executor);

        // 2、sku图片信息 pms_sku_images
        CompletableFuture<Void> imgFuture = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> skuImages = skuImagesService.list(new LambdaQueryWrapper<SkuImagesEntity>().eq(SkuImagesEntity::getSkuId, skuId));
            skuItemVo.setSkuImages(skuImages);
        }, executor);

        // 3、sku对应spu的销售属性组合
        CompletableFuture<Void> saleFuture = infoFuture.thenAcceptAsync(info -> {
            List<SkuItemSaleAttrVo> skuSaleAttrs = skuSaleAttrValueService.getSaleAttrsBySupId(info.getSpuId());
            skuItemVo.setSkuSaleAttrs(skuSaleAttrs);
        }, executor);

        // 4、spu的介绍 pms_spu_info_desc
        CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync(info -> {
            SpuInfoDescEntity spuDesp = spuInfoDescService.getById(info.getSpuId());
            skuItemVo.setSpuDesp(spuDesp);
        }, executor);

        // 5、spu的规格参数信息，即所有属性值
        CompletableFuture<Void> baseAttrFuture = infoFuture.thenAcceptAsync(info -> {
            List<SpuItemAttrGroupVo> spuGroupAttrs = attrGroupService.getAttrGroupWithAttrsBySpuId(info.getSpuId(), info.getCatalogId());
            skuItemVo.setSpuGroupAttrs(spuGroupAttrs);
        }, executor);

        // ------ 等待所有异步任务结束
        CompletableFuture.allOf(imgFuture, saleFuture, descFuture, baseAttrFuture).get();

        return skuItemVo;
    }
}